package com.guiwu.gteway.industryapp.Device.View;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.guiwu.gteway.industryapp.Device.MyAdapter.GateWayExpandAdapter;
import com.guiwu.gteway.industryapp.Device.module.GateWay;
import com.guiwu.gteway.industryapp.Device.module.GateWayGroup;
import com.guiwu.gteway.industryapp.MainActivity;
import com.guiwu.gteway.industryapp.R;
import com.guiwu.gteway.industryapp.util.MyLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 乔冠 on 2017/12/21. 设备选择碎片
 */

public class DeviceListFragment extends Fragment implements ExpandableListView.OnChildClickListener{

    private ExpandableListView GatewayGroupsView;
    private List<GateWayGroup> gateWayGroupList = new ArrayList<>();
    private GateWayExpandAdapter expandAdapter;
    private MainActivity activityParent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityParent = (MainActivity) getActivity();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.gateway_groups_layout,container,false);
        GatewayGroupsView = v.findViewById(R.id.expand_listview);
        GatewayGroupsView.setOnChildClickListener(this);
        return v;
    }

    public void updateUI(){
        paddingData();
//        if (expandAdapter != null)
//            expandAdapter.notifyDataSetChanged();
//        else {
            expandAdapter = new GateWayExpandAdapter(getLayoutInflater(),gateWayGroupList,getContext());
            GatewayGroupsView.setAdapter(expandAdapter);
//        }
    }

    //填充网关组数据
    private void paddingData(){
        gateWayGroupList = activityParent.getmGateWayGroups();
        //MyLog.i("MainActivity", " paddingData" + gateWayGroupList);
        if (gateWayGroupList == null)
            return;
    }


    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        List<GateWay> gateWayList = gateWayGroupList.get(groupPosition).getGateWays();
        GateWay clickGateWay = gateWayList.get(childPosition);
        if (clickGateWay.getIsOnline() == 0)
            return false;
        MonitorActivity.actionStart(getActivity(),clickGateWay);
        return false;
    }


}
